package com.pms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pms.pojo.Department;
import com.pms.pojo.DeptTransfer;
import com.pms.pojo.DeptTurn;
import com.pms.pojo.Employees;
import com.pms.pojo.FindRequire;
import com.pms.pojo.Require;
import com.pms.service.PostService;


@RequestMapping("/post")
@Controller
public class PostHandler {
	
	@Resource(name="postService")
	private PostService postService;
	
	
	public void setpostService(PostService postService) {
		this.postService = postService;
	}

//  进入部门调动界面
	@RequestMapping("/out")
	 public String out(Model  model) {
		List<Employees> employees = this.postService.getEmployees();
		List<Department> departments = this.postService.getDepartment();
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		return "post_index";
	}
//	按条件查询员工表
	@RequestMapping("/getRequire")
	public String getRequire(Require require,Model model) {
		
		List<Department> departments = this.postService.getDepartment();
		List<Employees> employees = this.postService.getRequireEmployees(require);
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		return "post_index";
	}
//	进入修改界面
	@RequestMapping("/getUpdate")
	public String updateDeptartment(Integer emp_id,Model model) {
		Employees employees = this.postService.getEmployeeById(emp_id);
		List<Department> departments = this.postService.getDepartment();
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		
		return "post_transfer";
	}
//	修改和添加
	@RequestMapping("/updateAndAdd")
	public String updateAndAdd(DeptTransfer deptTransfer,Model model) {
		//查询岗位下有多少人
		int count = this.postService.getlist(deptTransfer);
		int post_count = this.postService.getPostList(deptTransfer);
		if(count>=post_count) {
			model.addAttribute("success", "error");
			return "post_transfer";
		}
		this.postService.updateEmployee(deptTransfer);
		Department department =  this.postService.getDept(deptTransfer.getDt_dept_a());
		deptTransfer.setDt_dept_name(department.getDept_name());
//		System.out.println(department.toString());
//		System.out.println(deptTransfer.toString());
		this.postService.addDeptTransfer(deptTransfer);
		model.addAttribute("success", "success");
		//return "dept_index";
		return "post_transfer";
	}
	
	
	@RequestMapping("/getDeptTranfer")
	public String getDeptTranfer(Model model) {
		List<DeptTurn> deptTurn = this.postService.getDeptTranfer();
		model.addAttribute("deptTurn", deptTurn);
		return "post_find";
	}
	@RequestMapping("/getByRequire")
	public String getByRequire(FindRequire findrequire,Model model) {
		List<DeptTurn> deptTurn = this.postService.getByRequire(findrequire);
		model.addAttribute("deptTurn", deptTurn);
		System.out.println(findrequire.toString());
		return "post_find";
	}
	
	
}
